//
//  TilePieceList.m
//  MapEdit
//
//  Created by Steven Canfield on 16/06/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "TilePieceList.h"
#import "ColorPalette.h"
#define DEBUG
@implementation TilePieceList
- (id)initWithData:(NSData *)data palette:(ColorPalette *)palette {
	self = [super init];
	if( self ) {
		_piecesBitmapData = [[NSMutableArray alloc] init];
		_flippedPiecesBitmapData = [[NSMutableArray alloc] init];
		_pieces = malloc( sizeof( tile_piece_t * ) * [data length] / 64 );
		_nPieces = 0;
		
	//	_piecesInfo = malloc( sizeof( tile_piece_tags_t *) * [data length] / 64 );
		
		int dataIndex = 0;
		while( dataIndex < [data length] ) {
			tile_piece_t * piece = malloc( sizeof ( tile_piece_t) );
			[data getBytes:piece range:NSMakeRange( dataIndex, sizeof( tile_piece_t ) )];
			
		//	NSBitmapImageRep * imageRep = [self imageRepFromTilePiece:piece palette:palette];
	//		[_piecesBitmapData addObject:imageRep];
			
	//		NSBitmapImageRep * flippedImageRep = [self flippedImageRepFromTilePiece:piece palette:palette];
	//		[_flippedPiecesBitmapData addObject:flippedImageRep];
			
			_pieces[_nPieces] = piece;
			/*
			_piecesInfo[_nPieces] = malloc( sizeof( tile_piece_tags_t) );
			_piecesInfo[_nPieces]->needsXTranslation = NO;
			_piecesInfo[_nPieces]->needsYTranslation = NO;
			_piecesInfo[_nPieces]->needsXReflection = YES;
			_piecesInfo[_nPieces]->needsYReflection = NO;
			_piecesInfo[_nPieces]->needsRotateCW = NO;
			*/
			_nPieces++;
			
			
			dataIndex += sizeof( tile_piece_t );
		}
		DebugLog(@"Loaded %i Tile Pieces", _nPieces);
	}
	return self;
}

- (tile_piece_tags_t *)pieceInfoAtIndex:(int)index {
	return _piecesInfo[ index ];
}

- (NSBitmapImageRep * )imageRepFromTilePiece:(tile_piece_t *)piece palette:(ColorPalette *)palette
{
	NSImage * image = [[NSImage alloc] initWithSize:NSMakeSize(8,8)];
	[image lockFocus];
	NSBitmapImageRep * imageRep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];
	int x, y;
	for( x = 0; x < 8; x++ ) {
		for( y = 0; y < 8; y++ ) {
			[imageRep setColor:[palette colorAtIndex:(*piece).pixels[x][y]] atX:x y:y];
		}
	}
	[image unlockFocus];
	[imageRep retain];
	[image release];
	return imageRep;
}

- (NSBitmapImageRep * )flippedImageRepFromTilePiece:(tile_piece_t *)piece palette:(ColorPalette *)palette
{
	NSImage * image = [[NSImage alloc] initWithSize:NSMakeSize(8,8)];
	[image lockFocus];
	NSBitmapImageRep * imageRep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];
	int x, y;
	for( x = 0; x < 8; x++ ) {
		for( y = 0; y < 8; y++ ) {
			[imageRep setColor:[palette colorAtIndex:(*piece).pixels[x][y]] atX:(7-y) y:(7-x)];
		}
	}
	[image unlockFocus];
	[imageRep retain];
	[image release];
	return imageRep;
}


- (NSBitmapImageRep *)bitmapForPieceAtIndex:(int)index {
	if( index < 0 || index > [_piecesBitmapData count] ) {
		DebugLog(@"Piece Array Index %i Beyond Bounds %i", index, [_piecesBitmapData count]  );
	}
	
	return [_piecesBitmapData objectAtIndex:index];
}

- (NSBitmapImageRep *)flippedBitmapForPieceAtIndex:(int)index {
	if( index < 0 || index > [_flippedPiecesBitmapData count] ) {
		DebugLog(@"Piece Array Index %i Beyond Bounds %i", index, [_flippedPiecesBitmapData count]  );
	}
	
	return [_flippedPiecesBitmapData objectAtIndex:index];
}


- (tile_piece_t)pieceAtIndex:(int)index {
	if( index < 0 || index >= _nPieces ) {
		DebugLog(@"Piece Array Index %i Beyond Bounds %i", index, _nPieces );
		tile_piece_t returnVal;
		return returnVal;
	}
	return *_pieces[index];
}


- (void)dealloc {
	[_piecesBitmapData release];
	[_flippedPiecesBitmapData release];
	int i;
	for( i = 0; i < _nPieces; i++ ) {
		free( _pieces[i] );
	}
	[super dealloc];
}
@end
